package com.study.service.impl;

import com.alicp.jetcache.Cache;
import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.CreateCache;
import com.study.model.MsgCode;
import com.study.service.MsgCodeService;
import com.study.utils.MsgCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @desc:
 * @author: panxiyi
 * @date: 2023/5/3 11:52 下午
 */

@Service
public class MsgCodeServiceImpl implements MsgCodeService {

    @Autowired
    private MsgCodeUtils msgCodeUtils;

    @CreateCache(name = "jetCache_", expire = 10, timeUnit = TimeUnit.SECONDS, cacheType = CacheType.LOCAL)
    private Cache<String, String> jetCache;


    @Override
    public String genCode(String tele) {
        String code = msgCodeUtils.generator(tele);
        jetCache.put(tele, code);
        return code;
    }

    @Override
    public boolean checkCode(MsgCode msgCode) {
        String code = jetCache.get(msgCode.getTele());
        return msgCode.getCode().equals(code);
    }
}
